#include <ngx_core.h>
#include "ngx_core_ce.h"


static void *ngx_core_ce_creat_conf(ngx_cycle_t *cycle);
static char *ngx_core_ce_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_core_ce_init_conf(ngx_cycle_t *cycle, void *conf);

static ngx_command_t ngx_core_ce_commands[] = {
    { ngx_string("conf_example"),
      NGX_MAIN_CONF | NGX_DIRECT_CONF | NGX_CONF_TAKE1,
      ngx_core_ce_block,
      0,
      0,
      NULL },

    ngx_null_command
};

static ngx_core_module_t  ngx_core_ce_module_ctx = {
    ngx_string("ngx_core_ce"),
    ngx_core_ce_creat_conf,
    ngx_core_ce_init_conf
};

ngx_module_t  ngx_core_ce_module = {
    NGX_MODULE_V1,
    &ngx_core_ce_module_ctx,                  /* module context */
    ngx_core_ce_commands,                     /* module directives */
    NGX_CORE_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};


static void *ngx_core_ce_creat_conf(ngx_cycle_t *cycle){
    ce_conf_t *cecf;
    cecf = ngx_palloc(cycle->pool,sizeof(ce_conf_t));
    if(cecf == NULL){
        return NULL;
    }

    cecf->ce_numbers = NGX_CONF_UNSET_UINT;
    return cecf;
}

static char *ngx_core_ce_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){
    ce_conf_t *cecf = conf;
    ngx_str_t *value;

    if(cecf->ce_numbers != NGX_CONF_UNSET_UINT){
       return "is duplicate";
    }

    value = cf->args->elts;

    cecf->ce_numbers = ngx_atoi(value[1].data,value[1].len);
    if (cecf->ce_numbers == (ngx_uint_t) NGX_ERROR) {
        ngx_conf_log_error(NGX_LOG_EMERG,cf,0,
                           "invalid number \"%V\"",&value[1]);
        return  NGX_CONF_ERROR;
    }

    return NGX_CONF_OK;
}

static char *ngx_core_ce_init_conf(ngx_cycle_t *cycle, void *conf){
    ce_conf_t *cecf = conf;

    if(cecf->ce_numbers == NGX_CONF_UNSET_UINT){
        ngx_log_error(NGX_LOG_EMERG,cycle->log,0,"do not set the ce_numbers_conf value,we set it's value -100");
    }
    else{
        ngx_log_error(NGX_LOG_EMERG,cycle->log,0,"the ce_numbers_conf value is \"%ui\"",cecf->ce_numbers);
    }

    return NGX_CONF_OK;
}
